package com.lori.netshop.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lori.netshop.pojo.TbUser;
import com.lori.netshop.service.UserService;
import com.lori.netshop.util.CookieUtils;
import com.lori.netshop.util.NetshopResult;
import com.lori.netshop.util.StringUtil;

/**
 * 用户登录控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userServiceimp;
	/**
	 * 用户数据校验
	 */
	@RequestMapping("/check/{param}/{type}")
	@ResponseBody
	public Object checkUser(@PathVariable String param,@PathVariable Integer type,String callback){
		//调用service获得数据
		NetshopResult result = userServiceimp.checkUser(param, type);
		//封装result，返回jsonp格式数据
		if(!StringUtil.isEmpty(callback)){
			MappingJacksonValue mjv = new MappingJacksonValue(result);
			mjv.setJsonpFunction(callback);
			return mjv;
		}
		return result;
	}
	/**
	 * 用户注册
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Object registerUser(TbUser user,String callback){
		NetshopResult result = null;
		//校验数据
		//校验用户名
		if(StringUtil.isEmpty(user.getUsername())){
			result = NetshopResult.build(400, "用户名不能为空");
		}else{
			//用户名是否存在于数据库
			NetshopResult checkUser = userServiceimp.checkUser(user.getUsername(), 1);
			if(checkUser.getStatus() == 200 && !(boolean)checkUser.getData()){
				result = NetshopResult.build(400, "用户名已经存在");
			}
		}
		//校验手机号码
		if(StringUtil.isEmpty(user.getPhone())){
			result = NetshopResult.build(400, "手机号码不能为空");
		}else{
			//手机号码是否存在于数据库
			NetshopResult checkUser = userServiceimp.checkUser(user.getPhone(), 2);
			if(checkUser.getStatus() == 200 && !(boolean)checkUser.getData()){
				result = NetshopResult.build(400, "手机号码已经存在");
			}
		}
		/*//校验email
		if(StringUtil.isEmpty(user.getEmail())){
			result = NetshopResult.build(400, "email不能为空");
		}else{
			//用户名是否存在于数据库
			NetshopResult checkUser = userServiceimp.checkUser(user.getEmail(), 3);
			if(checkUser.getStatus() == 200 && (boolean)checkUser.getData()){
				result = NetshopResult.build(400, "email已经存在");
			}
		}*/
		if(result != null){
			if(StringUtil.isEmpty(callback)){
				return result;
			}else{
				//包装返回数据解决js跨域问题
				MappingJacksonValue mjv = new MappingJacksonValue(result);
				mjv.setJsonpFunction(callback);
				return mjv;
			}
		}
		//调用service
		result = userServiceimp.registerUser(user);
		if(StringUtil.isEmpty(callback)){
			return result;
		}else{
			//包装返回数据解决js跨域问题
			MappingJacksonValue mjv = new MappingJacksonValue(result);
			mjv.setJsonpFunction(callback);
			return mjv;
		}
	}
	/**
	 * 用户登录
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Object login(String username,String password,String callback,HttpServletRequest request,
			HttpServletResponse response){
		NetshopResult result = userServiceimp.loginUser(username, password);
		//判断是否登录成功
		if(result.getStatus() == 200){
			CookieUtils.setCookie(request, response, "token", (String)result.getData(),true);
		}
		if(!StringUtil.isEmpty(callback)){
			MappingJacksonValue mjk = new MappingJacksonValue(result);
			mjk.setJsonpFunction(callback);
			return mjk;
		}
		return result;
	}
	/**
	 * 根据token查询用户数据
	 */
	@RequestMapping(value="/token/{token}",method=RequestMethod.GET)
	@ResponseBody
	public Object checkByToken(@PathVariable String token,String callback){
		NetshopResult result = userServiceimp.checkByToken(token);
		if(!StringUtil.isEmpty(callback)){
			MappingJacksonValue mjk = new MappingJacksonValue(result);
			mjk.setJsonpFunction(callback);
			return mjk;
		}else{
			return result;
		}
	}
	/**
	 * 安全退出
	 */
	@RequestMapping(value="/logout/{token}",method=RequestMethod.GET)
	@ResponseBody
	public Object loginOut(@PathVariable String token,String callback){
		NetshopResult result = userServiceimp.loginOut(token);
		if(!StringUtil.isEmpty(callback)){
			MappingJacksonValue mjk = new MappingJacksonValue(result);
			mjk.setJsonpFunction(callback);
			return mjk;
		}else{
			return result;
		}
		
	}
	
	
	
	
	
	
	
	
	
	
}
